package it.sevenbits.view;

import it.sevenbits.model.Database;

import java.util.ArrayList;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;


public class WebOut implements Output {
    
    
    ResourceBundle bundle = null;
    private static final Logger log4= Logger.getLogger(WebOut.class);
    private Journal journ;
    
    /**
     * Constructor
     */
    WebOut() {
        bundle = ResourceBundle.getBundle("logging");
        journ = new Journal();
    }

    /**
     * Parses request from user, calls responded method in database and 
     * returns responded values 
     */
    public ArrayList<String> resp(String reqArray[]) {

        String type = reqArray[0];
        String req = reqArray[1];
        journ.addToJournal(reqArray[0] + " " + reqArray[1]);
        ArrayList<String> result = new ArrayList<String>();
        try {
            Database base = new Database();
            boolean done = false;
            if (type.equalsIgnoreCase("select")) {
                String sel[] = base.select(req);
                if (sel != null) {

                    for (int i = 0; i < sel.length; i++) {
                        result.add(sel[i]);
                    }
                } else {
                    result.add("Select: item not found");
                }
            } 
            else if (type.equalsIgnoreCase("insert")) {
                String insName = req.substring(0, req.indexOf("\r\n"));
                insName = insName.trim();
                String insNum = req.substring(req.indexOf("\r\n") + 2);
                insNum = insNum.trim();
                String ins[] = {
                    insName, insNum
                };
                done = base.insert(ins);
                if (done) {
                    result.add(insName + " inserted");
                }
            } else if (type.equalsIgnoreCase("update all")) {
                int indexOfEnter = req.indexOf("\r\n");
                String oldName = req.substring(0, indexOfEnter);
                String newName = req.substring(indexOfEnter + 2,
                    req.lastIndexOf("\r\n"));
                String newNum = req.substring(req.lastIndexOf("\r\n") + 2);
                String upd[] = {
                    oldName, newName, newNum
                };
                done = base.update(upd);
                if (done) {
                    result.add(newName + " updated");
                }
            } else if (type.equalsIgnoreCase("update")) {
                int indexOfEnter = req.indexOf("\r\n");
                String oldName = req.substring(0, indexOfEnter);
                String newName = req.substring(indexOfEnter + 2,
                    req.lastIndexOf("\r\n"));
                String newNum = req.substring(req.lastIndexOf("\r\n") + 2);
                String upd[] = {
                    oldName, newName, newNum
                };
                done = base.updateOne(upd);
                if (done) {
                    result.add(newName + " updated");
                }
            } else if (type.equalsIgnoreCase("delete all")) {
                done = base.delete(req);
                if (done) {
                    result.add(req + " deleted");
                }
            } else if (type.equalsIgnoreCase("delete")) {
                done = base.deleteOne(req);
                if (done) {
                    result.add(req + " deleted");
                }
            } else {
                result.add("Unknown request.");
            }
            journ.Replication();
        } catch (Exception e) {
            log4.error("error in output: " + e);
        }
        return result;
    }
}